package com.rodagroup.study.modules.grpc.demo.provider.app.internal.impl;

import com.rodagroup.study.modules.grpc.demo.proto.hello.Gender;
import com.rodagroup.study.modules.grpc.demo.proto.hello.SayHelloGrpc.SayHelloImplBase;
import com.rodagroup.study.modules.grpc.demo.proto.hello.SayHelloRequest;
import com.rodagroup.study.modules.grpc.demo.proto.hello.SayHelloResponse;
import io.grpc.stub.StreamObserver;

/**
 * @author jake, {@literal <jake.kim@rodagroup.cn>}
 * @date 2022-07-07.
 */
public class HelloInternalService extends SayHelloImplBase {

  @Override
  public void sayHello(SayHelloRequest request, StreamObserver<SayHelloResponse> responseObserver) {
//    super.sayHello(request, responseObserver);


    SayHelloResponse response = null;

    int age = request.getAge();
    String name = request.getName();
    Gender gender = request.getGender();

    try {
      response = SayHelloResponse.newBuilder()
          .setHelloMessage(gender.name() + "的" + name + "说自己" + age + "岁了！")
          .build();
    } catch (Exception e) {
      responseObserver.onError(e);
    } finally {
      responseObserver.onNext(response);
    }
    responseObserver.onCompleted();
  }
}
